package com.chendx.store.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.PageResponseResult;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.goods.dto.PrefDto;
import com.chendx.model.goods.pojo.Pref;
import com.chendx.store.common.constants.goods.GoodsConstants;
import com.chendx.store.goods.mapper.PrefMapper;
import com.chendx.store.goods.service.PrefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chendx on 2022/4/19.
 */
@Service
public class PrefServiceImpl extends ServiceImpl<PrefMapper,Pref> implements PrefService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult findPage(PrefDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        LambdaQueryWrapper<Pref> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getCateId())){
            queryWrapper.eq(Pref::getCateId,dto.getCateId());
        }
        if (!StringUtils.isEmpty(dto.getState())){
            queryWrapper.eq(Pref::getState,dto.getState());
        }
        if (!StringUtils.isEmpty(dto.getType())){
            queryWrapper.eq(Pref::getType,dto.getType());
        }
        Page page = new Page(dto.getPage(),dto.getSize());
        IPage<Pref> result = page(page, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult findById(String id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Pref pref = getById(id);
        return ResponseResult.okResult(pref);
    }

    @Override
    public ResponseResult findAll() {
        Boolean flag = redisTemplate.hasKey(GoodsConstants.PREF_COUPON);
        List<Pref> prefs = new ArrayList<>();
        if (flag){
            List<Object> objectList = redisTemplate.boundHashOps(GoodsConstants.PREF_COUPON).values();
            prefs = JSON.parseArray(JSON.toJSONString(objectList), Pref.class);
        }else{
            prefs = list(Wrappers.<Pref>lambdaQuery()
                    .eq(Pref::getState, "1")
                    .ge(Pref::getEndTime, new Date())
                    .lt(Pref::getStartTime, new Date()));
            for (Pref pref : prefs){
                redisTemplate.boundHashOps(GoodsConstants.PREF_COUPON).put(pref.getId(),JSON.toJSONString(pref));
            }
        }
        return ResponseResult.okResult(prefs);
    }

    @Override
    public ResponseResult findByUsername() {
        return null;
    }
}
